<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;
use think\Request;

class zhj extends Admin {
  protected $zhj;
  protected $WxUser;
  public function _initialize() {
		parent::_initialize();
    $this->zhj = model('zhj');
    $this->WxUser = model('WxUser');
	}

  public function index($keyword=null) {
    $list = $this->zhj->where('order_number|name','like','%'.$keyword.'%')->order('id desc')->paginate(10);
		$this->assign('list', $list);
		$this->setMeta('导入记录');
		return $this->fetch();
	}



  //导入智慧记
  public function import(Request $request){

    vendor('phpoffice.phpexcel.Classes.PHPExcel');
    vendor('phpoffice.phpexcel.Classes.PHPExcel.IOFactory');
    // 获取表单上传文件
           $file = $request->file('file');
           if (empty($file)) {
               $this->error('请选择上传文件');
           }
           // 移动到框架应用根目录/public/uploads/ 目录下
           $info = $file->validate(['ext'=>'xls'])->move(ROOT_PATH . 'uploads' . DS . 'zhjdata');
           if ($info) {

              // echo $info->getRealPath();
           } else {
               // 上传失败获取错误信息
               $this->error($file->getError());
           }



    //$file =  ROOT_PATH . DS . '1.xls';
    $file =  $info->getRealPath();
    $objPHPExcel = \PHPExcel_IOFactory::load($file);

    $sheet = $objPHPExcel->getSheet(0);

    //获取行数与列数,注意列数需要转换
    $highestRowNum = $sheet->getHighestRow();
    $highestColumn = $sheet->getHighestColumn();
    $highestColumnNum = \PHPExcel_Cell::columnIndexFromString($highestColumn);

    //取得字段，这里测试表格中的第一行为数据的字段，因此先取出用来作后面数组的键名
    $filed = array();
    for($i=0; $i<$highestColumnNum;$i++){
    	$cellName = \PHPExcel_Cell::stringFromColumnIndex($i).'1';
    	$cellVal = $sheet->getCell($cellName)->getValue();//取得列内容
    	$filed []= $cellVal;
    }

    //开始取出数据并存入数组
    $data = array();
    for($i=2;$i<=$highestRowNum;$i++){//ignore row 1
    	$row = array();
    	for($j=0; $j<$highestColumnNum;$j++){
    	  $cellName = \PHPExcel_Cell::stringFromColumnIndex($j).$i;
    	  $cellVal = $sheet->getCell($cellName)->getValue();

          $row[ $j ] = $cellVal;


    	}
    	$data []= $row;
    }

    foreach ($data as $key => $value) {
        $order = $this->zhj->where('order_number',$value[1])->find();
        $wid = $this->WxUser->where('zhj_name',$value[2])->value('id');

        $zhjdata['order_number'] = $value[1];
        $zhjdata['name'] = $value[2];
        $zhjdata['integral'] = $value[10];
        $zhjdata['w_id'] = $wid;
        if($zhjdata['w_id']){
          $zhjdata['status'] = 1;
        }else{
          $zhjdata['status'] = 0;
        }
        if(!$order['id']){
          if($wid){
            $this->WxUser->where('id', $zhjdata['w_id'])->setInc('integral', $zhjdata['integral']);
          }
          $this->zhj->create($zhjdata);
        }elseif ($order['status']==0 && $wid) {
            $zhjdata['status'] = 1;
            $this->WxUser->where('id', $zhjdata['w_id'])->setInc('integral', $zhjdata['integral']);
            $zhjdata['id'] = $order['id'];
            $this->zhj->update($zhjdata);
        }


    }


     $this->success('导入成功！');



  }

}

 ?>
